package ysf.com.difficulty;

/**
 * @author ysf
 * @date 2021/6/13 10:15
 */
public class FifthDemos {


    public static void main(String[] args) {
        String string = "abcddc";
        String string2 = "abbaabba";
        String strings = "abc435cba";
        String s = longestPalindrome(string);
        System.out.println(s);

    }


    public static String longestPalindrome(String s){
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            String str1 = palindrome(s,i,i);
            String str2 = palindrome(s,i,i+1);

            // 三元运算符 ，判断为真时取冒号前面的值，为假时取冒号后面的值
            str = str.length() > str1.length() ? str : str1;
            str = str.length() > str2.length() ? str : str2;
        }
        return str;
    }
    //辅助函数 寻找回文串
    private static String palindrome(String s , int left ,int right){
        while (left >= 0 && right < s.length()){
            char c1 = s.charAt(left);
            char c2 = s.charAt(right);
            if(c1 == c2){
                left --;
                right ++;
            }else {
                break;
            }
        }
        int start = left + 1;
        int end = right;
        String substring = s.substring(start, end);
        return substring;
    }

}
